//
//  KXHeaderView.swift
//  KXBook
//
//  Created by  on 2022/9/9.
//

import UIKit

class KXHeaderView: UICollectionReusableView {
    @IBOutlet var bgBtnView: KXTypeSelectBtn!
    @IBOutlet var input1: KXTextField!
    @IBOutlet var input2: KXTextField!
    @IBOutlet var title: UILabel!
    @IBOutlet var titleTop: NSLayoutConstraint!
    var block:((_ pay:KXAddType, _ money:String, _ desri:String)->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    ///type = 0  全显示。 type == 1 显示title
    var style : Int = 0 {
        didSet{
            self.bgBtnView.isHidden = style == 1
            self.input1.isHidden = style == 1
            self.input2.isHidden = style == 1
            self.titleTop.constant = style == 1 ? 20 : 202
        }
    }
    var model:KXRecord = KXRecord(){
        didSet{
            self.bgBtnView.type = model.type.isPay == 1 ? .pay : .In
            self.input1.text = model.money
            self.input2.text = model.descrip           
        }
    }
}

extension KXHeaderView : UITextFieldDelegate{
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
        if textField == self.input1{
            textField.keyboardType = .numberPad
        }else if textField == self.input2{
            textField.keyboardType = .default
        }
        return  true
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == self.input1 && (textField.text!.contains(".")){
            return false
        }
        return true
    }
    func textFieldDidChangeSelection(_ textField: UITextField){
        weak var weakself = self
        weakself?.block?(weakself!.bgBtnView.type, weakself!.input1.text!,weakself!.input2.text!)
    }
    
}
